package com.oms.web;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oms.service.local.CustomerService;
import com.oms.service.local.UserService;
import com.oms.vo.first.User;
import com.oms.vo.second.Customer;

@RestController
@RefreshScope // 用于客户端接受服务端的更新消息
public class HelloWordController {
	Logger logger = LoggerFactory.getLogger(HelloWordController.class);
	@Value("${book.name}")
	private String bookName;
	@Value("${user.name}")
	private String userName;

	@Autowired
	private CustomerService customerService;
	@Autowired
	private UserService userService;

	@RequestMapping("/demo/test")
	public String test() {
		return userName + ">>>" + bookName;
	}

	@RequestMapping("/book/name")
	public String getBookName() {
		return bookName;
	}

	@GetMapping("/user/{id}")
	public User getUserById(@PathVariable("id") Long id) {
		logger.debug("getUserById param>>{}", id);
		User us = userService.getUserById(id);
		logger.debug("getUserById result>>{}", us.toString());
		return us;
	}

	@PostMapping("/user/add")
	public Long addUser(@RequestBody User us) {
		us = userService.addUser(us);
		return us.getId();
	}

	@DeleteMapping("/user/{id}")
	public String delUser(@PathVariable("id") Long id) {
		userService.delUser(id);
		return "ok";
	}

	
	@GetMapping("/user/page")
	public Page<User> pageUser(@RequestParam(name = "pageNum", defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(name = "rowNum", defaultValue = "1", required = false) Integer rowNum) {
		Sort sort = new Sort(Sort.Direction.DESC, "id");
		Pageable pageable = new PageRequest(pageNum, rowNum, sort);
		Page<User> page = userService.findAll(pageable);
		return page;
	}

	@PostMapping("/mybatis/us")
	public PageInfo<User> getUserPage(@RequestBody Map<String,Integer> map) {
		Integer pageNum=map.get("pageNum");
		Integer pageSize=map.get("pageSize");
		
		PageHelper.startPage(pageNum, pageSize);
		List<User>  us=userService.findUsers();
		PageInfo<User> info=new PageInfo<>(us);
		return info;
	}
	
	@GetMapping("/customer/{id}")
	public Customer getCusById(@PathVariable("id") Long id) {
		return customerService.getById(id);
	}

}
